local Msg = require("core.Msg")
local WSConfig = require("excel.CopyWushuang").wushuang
local Grid = require("bag.Grid")
local VipLogic = require("vip.VipLogic")
local VipDefine = require("vip.VipDefine")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local CopyL = require("copy.CopyL")
local CopyExcel = require("excel.Map").copy
local Log = require("common.Log")
local ShopHelp = require("shop.ShopHelp")
local Banshu = require("common.Banshu")

--打开无双战域主界面
function open(human)
  local sendMsg = Msg.gc.GC_COPY_WUSHUANG_CONFIG
  sendMsg.config[0] = #WSConfig
  local cache = {}
  for i=1, #WSConfig do
    sendMsg.config[i].name = WSConfig[i].name
    sendMsg.config[i].reward[0] = #WSConfig[i].reward
    for j=1, #WSConfig[i].reward do
      Grid.create(cache, WSConfig[i].reward[j][1], WSConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.config[i].reward[j], 0, human)
    end
    sendMsg.config[i].needTiaozhan = CopyExcel[WSConfig[i].copyId].enterLoss[1][2]
    sendMsg.config[i].sbBodyId = WSConfig[i].sbBodyId
    local copy = CopyExcel[WSConfig[i].copyId]
    local group = CopyL.group2Copy[copy.group]
    for j = 1, #group do
       local config = CopyExcel[group[j]]
       sendMsg.config[i].copyIds[j] = group[j]
       sendMsg.config[i].levels[j] = config.level
       sendMsg.config[i].difficult[j] = config.difficult            
    end
    sendMsg.config[i].copyIds[0] = #group
    sendMsg.config[i].levels[0] = #group
    sendMsg.config[i].difficult[0] = #group
  end
  sendMsg.addTZNeedRMB = WSConfig[1].addNeedRMB 
  Msg.send(sendMsg, human.fd)
  sendTZTimes(human)
end
--发送剩余挑战值
function sendTZTimes(human)
  local sendMsg = Msg.gc.GC_COPY_WUSHUANG_REMAIN_TIMES
  local canAddTimes = VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_WUSHUANG)
  sendMsg.curTZ = human.db.remainTZ
  sendMsg.totalTZ = WSConfig[1].totalTZ + human.db.wsAddTimes * WSConfig[1].addTZZ
  sendMsg.remainTimes = canAddTimes - human.db.wsAddTimes
  Msg.send(sendMsg, human.fd)
end
--增加挑战值
function addTimes(human)
  local canAddTimes = VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_WUSHUANG)
  if canAddTimes - human.db.wsAddTimes <= 0 and canAddTimes >= 3 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.COPY_WUSHUANG_CANNOT_ADD)
  elseif canAddTimes - human.db.wsAddTimes <= 0 then
    if Banshu.IS_BANSHU then
      return
    else
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.COPY_WUSHUANG_VIP_NEED_ADD)
    end
  end
  if not ObjHuman.checkYuanbaoEnough(human, WSConfig[1].addNeedRMB) then
    return ShopHelp.onNoRMB(human)
  end
  ObjHuman.decYuanbao(human, WSConfig[1].addNeedRMB, "copy_add")
  human.db.wsAddTimes = human.db.wsAddTimes + 1
  human.db.remainTZ = human.db.remainTZ + WSConfig[1].addTZZ
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.COPY_WUSHUANG_ADD_SUCCESS)
  sendTZTimes(human)
  Log.write(Log.LOGID_OSS_WUSHUANG_COPY_ADD,human._id, human.account, human.name,human.db.lv,human.db.wsAddTimes )
end
--每日24点挑战值重置
function setTZValue(human)
  human.db.remainTZ = WSConfig[1].totalTZ
  human.db.wsAddTimes = 0
  sendTZTimes(human)
end